﻿using EA;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UMLSynthesizer.EAUMLSynthesizerPlugin.View;
using UMLSynthesizer.EAUMLSynthesizerPlugin.ViewModel;

namespace UMLSynthesizer.EAUMLSynthesizerPlugin.ExtensionMethods
{
    static class EAExtension
    {
        #region Extension methods

        /// <summary>
        /// Sets the state of the menu depending if there is an active project or not
        /// </summary>
        /// <param name="repository">The repository to analyze</param>
        /// <returns>True if is open, false otherwise</returns>
        public static bool IsFileProjectOpen(this Repository repository)
        {
            try
            {
                var c = repository.Models;
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Select a working file project, opening or creating it
        /// </summary>
        /// <param name="repository">The repository to modify</param>
        /// <returns>True if the file has been opened/created, false otherwise</returns>
        public static bool SelectWorkingFileProject(this Repository repository)
        {
            //Show the Project selector dialog
            var selectProjectWindow = new SelectProjectWindow();
            var selectProjectViewModel = new SelectProjectViewModel();

            EventHandler handler = null;
            handler = delegate
            {
                selectProjectViewModel.RequestClose -= handler;
                selectProjectWindow.Close();
            };
            selectProjectViewModel.RequestClose += handler;

            selectProjectWindow.DataContext = selectProjectViewModel;
            selectProjectWindow.ShowDialog();

            switch (selectProjectViewModel.ExitAction)
            {
                case SelectProjectViewModel.ExitActions.Open:
                    var openFileDiag = new OpenFileDialog();
                    openFileDiag.Title = "Open an Enterprise Architect Project";
                    openFileDiag.Filter = "Enterprise Architect Project (*.EAP)|*.eap";
                    if (openFileDiag.ShowDialog() == true)
                    {
                        try { repository.OpenFile(openFileDiag.FileName); }
                        catch (Exception) { return false; }
                        return true;
                    }
                    break;
                case SelectProjectViewModel.ExitActions.Create:
                    var saveFileDiag = new SaveFileDialog();
                    saveFileDiag.Title = "Create new Enterprise Architect Project";
                    saveFileDiag.Filter = "Enterprise Architect Project (*.EAP)|*.eap";
                    if (saveFileDiag.ShowDialog() == true)
                    {
                        try
                        {
                            if (System.IO.File.Exists(saveFileDiag.FileName))
                                System.IO.File.Delete(saveFileDiag.FileName);
                            repository.CreateModel(CreateModelType.cmEAPFromBase, saveFileDiag.FileName, 0);
                            repository.OpenFile(saveFileDiag.FileName);
                        }
                        catch (Exception) { return false; }
                        return true;
                    }
                    break;
            }
            
            return false;
        }
        /// <summary>
        /// Select the view where to work
        /// </summary>
        /// <param name="repository">The repository to modify</param>
        /// <returns>The selected view</returns>
        public static Package SelectWorkingView(this Repository repository)
        {
            //Check if the project is open, otherwise select it
            if (!repository.IsFileProjectOpen() &&
                !repository.SelectWorkingFileProject())
                return null;

            //Show the View selector dialog
            repository.Models.Refresh();
            var selectViewWindow = new SelectViewWindow();
            var selectViewViewModel = new SelectViewViewModel(
                from Package model in repository.Models
                from Package view in model.Packages
                orderby model.Name, view.Name
                select model.Name + "." + view.Name,

                from Package model in repository.Models
                orderby model.Name
                select model.Name);

            EventHandler handler = null;
            handler = delegate
            {
                selectViewViewModel.RequestClose -= handler;
                selectViewWindow.Close();
            };
            selectViewViewModel.RequestClose += handler;

            selectViewWindow.DataContext = selectViewViewModel;
            selectViewWindow.ShowDialog();
            if (!selectViewViewModel.ExitStateOK)
                return null;

            //Select or create the View inside the Model
            Package currentModel = (from Package model in repository.Models
                                    where model.Name == selectViewViewModel.SelectedModelName
                                    select model).First();
            if (selectViewViewModel.CreateNewView)
            {
                var selectedView = currentModel.Packages.AddNew(selectViewViewModel.SelectedViewName, "") as Package;
                selectedView.Update();
                currentModel.Packages.Refresh();
                return selectedView;
            }
            else
                return (from Package view in currentModel.Packages
                        where view.Name == selectViewViewModel.SelectedViewName
                        select view).First();
        }

        #endregion
    }
}
